# -*- coding: UTF-8 -*-
# @Time : 2021-02-01 17:07
# @File : Desired_Caps.py
import time
from utils.LogUtil import my_log
from utils.YmlReadel import YmlReadel
from config import Conf
from appium import webdriver
from common import MiniProgram
reader = YmlReadel(Conf.get_config_caps())
data = reader.data()
log = my_log('Desired_Caps')
program = MiniProgram


def appium_desired_caps(host, port):  # host, port, systemPort="8200"
    # 2、desired创建字典
    desired_caps = dict()
    # 3、platformName
    desired_caps['platformName'] = data['platformName']
    # 4、platformVersion
    desired_caps['platfromVersion'] = data['platfromVersion']
    # 5、deviceName
    desired_caps['deviceName'] = data['deviceName']

    # 6、启动程序的包名appPackage
    desired_caps["appPackage"] = data['appPackage']

    # 7、启动界面名appActivity
    desired_caps['appActivity'] = data['appActivity']

    # 解决中文
    desired_caps["unicodeKeyboard"] = data['unicodeKeyboard']
    desired_caps["resetKeyboard"] = data['resetKeyboard']

    # 获取toast automationName = uiautomator2
    desired_caps["automationName"] = data['automationName']

    # 不清除app里的原有数据
    desired_caps["noReset"] = data["noReset"]
    # chromeOptions 谷歌
    desired_caps["chromederiverExecutable"] = data['chromederiverExecutable']
    # 小程序进程
    desired_caps['chromeOptions'] = data['chromeOptions']
    # 解决并发测试
    # desired_caps["systemPort"] = systemPort
    # 8、http，连接appium服务器
    # print(desired_caps)
    driver = webdriver.Remote('http://%s:%s/wd/hub' % (host, port), desired_caps)
    """
    time.sleep(20)
    program.Start_MiniProgram(driver)
    time.sleep(1)
    program.window_handles(driver)
    
    size = driver.get_window_size()
    driver.swipe(size['width'] * 0.5, size['height'] * 0.3, size['width'] * 0.5, size['height'] * 0.8, 200)
    time.sleep(2)
    driver.find_element_by_android_uiautomator('text("测试优校通")').click()
    time.sleep(10)
    cons = driver.contexts
    log.info(cons)
    time.sleep(2)
    driver.switch_to.context('WEBVIEW_com.tencent.mm:appbrand0')    # WEBVIEW_com.tencent.mm:appbrand0
    handles = driver.window_handles
    log.debug('当前所有handles：{}'.format(handles))
    # 遍历handles
    for i in handles:
        driver.switch_to.window(i)
        if '测试优校通' in driver.page_source:
            break
     """
    # time.sleep(2)
    return driver


if __name__ == '__main__':
    appium_desired_caps('127.0.0.1', '4723')
